package cn.fanyamin.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.ToolService;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.Issue;
import cn.fanyamin.domain.User;

public class IssueAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(AddressAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		   
		
		User user = getUserFromSession(request);
        
		setCategories(request, UserCategory.CATEGORY_TYPE_TASK);

	    List<Issue> issueList = toolService.listIssue(user.getUserID());
	    request.setAttribute("issueList", issueList);
		logger.info("issueList: " + issueList);
		return new ActionForward("/WEB-INF/jsp/struts/tool/faq.jsp");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		Issue issue = getIssueFromForm(form);
		if(issue == null) {
		    return mapping.findForward("failure");
		}
		User user = getUserFromSession(request);
		issue.setUserID(user.getUserID());
		int issueID = toolService.createIssue(issue);
		issue.setIssueID(issueID);
		logger.info("create issue: " + issue);
		//return mapping.findForward("success");
		return new ActionForward("/WEB-INF/jsp/struts/tool/faq.jsp");
	}
	
    private Issue getIssueFromForm(ActionForm form) {
		Issue issue = new Issue();
		LazyValidatorForm issueForm = (LazyValidatorForm) form;

		issue.setIssueID(NumberUtils.toInt((String) issueForm.get("issueID")));
		issue.setProblem((String) issueForm.get("problem"));	
		issue.setCause((String) issueForm.get("cause"));	
		issue.setSolution((String) issueForm.get("solution"));
		issue.setComments((String)issueForm.get("comments"));
		
		return issue;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm issueFrom = (LazyValidatorForm)form;
        int issueID = NumberUtils.toInt((String)issueFrom.get("issueID"));
        Issue issue = toolService.retrieveIssue(issueID);
        request.setAttribute("issue", issue);
        return new ActionForward("/WEB-INF/jsp/struts/tool/faq.jsp");
    }
    
        
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        ToolService toolService = getWebApp().getToolService();
        Issue issue = getIssueFromForm(form);
        toolService.updateIssue(issue);
        //request.setAttribute("issueType", issue.getIssueType());
        logger.info("update issue, issue=" + issue);
        return new ActionForward("/WEB-INF/jsp/struts/tool/faq.jsp");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm issueFrom = (LazyValidatorForm)form;
        int issueID = NumberUtils.toInt((String)issueFrom.get("issueID"));
        toolService.deleteIssue(issueID);
        int issueType = NumberUtils.toInt(request.getParameter("issueType"));
        //request.setAttribute("issueType", issueType);
        logger.info("delete issue, issueID=" + issueID);
        //return mapping.findForward("success");
        return new ActionForward("/WEB-INF/jsp/struts/tool/faq.jsp");//redirect
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add issue");
    	
    	setCategories(request, UserCategory.CATEGORY_TYPE_TASK);
    	
    	return new ActionForward("/WEB-INF/jsp/struts/tool/issue.jsp");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm issueFrom = (LazyValidatorForm)form;
        int issueID = NumberUtils.toInt((String)issueFrom.get("issueID"));
        Issue issue = toolService.retrieveIssue(issueID);
        request.setAttribute("issue", issue);
        
        setCategories(request, UserCategory.CATEGORY_TYPE_TASK);
        
        return new ActionForward("/WEB-INF/jsp/struts/tool/issue.jsp");
    }
}
